package ar.edu.uba.fi.jmit.monitorengine.backend.client;

import gwtupload.client.IFileInput.FileInputType;
import gwtupload.client.SingleUploader;
import ar.edu.uba.fi.jmit.monitorengine.backend.client.view.LoadedFilesPanel;
import ar.edu.uba.fi.jmit.monitorengine.backend.client.view.MenuPanel;
import ar.edu.uba.fi.jmit.monitorengine.backend.client.view.ReceiverStatsPanel;
import ar.edu.uba.fi.jmit.monitorengine.backend.client.view.RecivedEventsPanel;
import ar.edu.uba.fi.jmit.monitorengine.backend.client.view.UploadFilesPanel;
import ar.edu.uba.fi.jmit.monitorengine.backend.client.view.VisualizersPanel;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class Monitorengine implements EntryPoint {
	
	private Widget mainPanel;
	
	private RecivedEventsPanel recivedEventsPanel = null;
	
	private LoadedFilesPanel loadedFilesPanel = null;
	
	private VisualizersPanel visualizersPanel = null;

	private ReceiverStatsPanel receiverStatsPanel = null;
	
	private SingleUploader singleUploader = null;
	
	@Override
	public void onModuleLoad() {
		GWT.log("On the new Monitorengine screen");
		this.loadMenu();
		this.initMainPanel();
	}

	private void initMainPanel() {
		GWT.log("Initializing Main Panel");
		RootPanel mainRootPanel = RootPanel.get("mainContainer");
		this.initDefaultPanel();
		DecoratorPanel decoratorPanel = new DecoratorPanel();
		decoratorPanel.setWidget(mainPanel);
		mainRootPanel.add(decoratorPanel);
		
	}

	private void initDefaultPanel() {
		mainPanel = new FlowPanel();
		mainPanel.setHeight("100%");
		mainPanel.setWidth("100%");
		((FlowPanel)mainPanel).add(new HTML("Welcome to MonitorEngine - Powered by JMit"));
		
	}

	private void loadMenu() {
		GWT.log("Loading Menu");
		RootPanel menuRootPanel = RootPanel.get("menu");
		menuRootPanel.add(new MenuPanel(this));
	}

	public void showRecivedMessagesPanel() {
		mainPanel = this.getRecivedMessagesPanel();
		refreshMainPanel();
	}
	
	public void showLoadedFilesPanel() {
		mainPanel = this.getLoadedFilesPanel();
		refreshMainPanel();
	}
	
	public RecivedEventsPanel getRecivedMessagesPanel() {
		if(recivedEventsPanel == null) {
			recivedEventsPanel = new RecivedEventsPanel();
		}
		return recivedEventsPanel;
	}
	
	public LoadedFilesPanel getLoadedFilesPanel() {
		if(loadedFilesPanel == null) {
			loadedFilesPanel = new LoadedFilesPanel();
		}
		return loadedFilesPanel;
	}

	private void refreshMainPanel() {
		RootPanel mainRootPanel = RootPanel.get("mainContainer");
		mainRootPanel.setWidth("100%");
		DecoratorPanel decoratorPanel = new DecoratorPanel();
		decoratorPanel.setWidget(mainPanel);
		mainRootPanel.clear();
		mainRootPanel.add(decoratorPanel);
	}

	public VisualizersPanel getVisualizersPanel() {
		if(visualizersPanel == null) {
			visualizersPanel = new VisualizersPanel();
		}
		return visualizersPanel;
	}

	public void showVisualizersPanel() {
		mainPanel = this.getVisualizersPanel();
		refreshMainPanel();
	}

	public ReceiverStatsPanel getReceiverStatsPanel() {
		if(receiverStatsPanel == null) {
			receiverStatsPanel  = new ReceiverStatsPanel();
		}
		return receiverStatsPanel;
	}

	public void showReceiverStats() {
		mainPanel = this.getReceiverStatsPanel();
		refreshMainPanel();
	}
	
	public SingleUploader getUploadFilePanel() {
		if (singleUploader == null) {
			singleUploader = new SingleUploader(FileInputType.BROWSER_INPUT, null, new Button(), new UploadFilesPanel());
		}
		return singleUploader;
	}
	
	public void showUploadFilePanel() {	
		mainPanel = this.getUploadFilePanel();
		refreshMainPanel();
	}
}
